
Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    var ordersrow;
    var ticketsrow;
    var oStatus = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [[ORDER_STATUS_YDD], [ORDER_STATUS_YCP], [ORDER_STATUS_YQX], [ORDER_STATUS_YFK], [ORDER_STATUS_YZF], [ORDER_STATUS_GQD]]
        }),
        name: 't_status',
        fieldLabel: '订单状态',
        resiable: true,
        typeAhead: true,
        width: 100,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 'oStatus',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        emptyText: '所有类型...',
        allowBlank: true,
        value: ''
    });
    var ticketStatus = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [[TICKET_STATUS_WQR], [TICKET_STATUS_QRCP], [TICKET_STATUS_LJCP]]
        }),
        name: 't_status',
        fieldLabel: '出票状态',
        resiable: true,
        typeAhead: true,
        width: 100,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 'ticketStatus',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        emptyText: '所有类型...',
        allowBlank: true,
        value: ''
    });
    var created_d = new Ext.form.Radio({
        fieldLabel: '日期类型',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '订单日期',
        checked: true,
        inputValue: 'created_at'
    });
    var fly_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '乘机日期',
        checked: false,
        inputValue: ''
    });
    var ticket_out_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '出票日期',
        checked: false,
        inputValue: 'ticket_out_date'
    });
    var send_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '送票日期',
        checked: false,
        inputValue: 'send_date'
    });
    
    var date_field = new Ext.form.DateField({
        fieldLabel: '日期查询',
        name: 'date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //anchor: '50%'
        width: 100,
        value: today
    });
    
    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (formPanel.form.isValid()) {
                var status = oStatus.getValue();
                var t_status = ticketStatus.getValue();
                var date = date_field.getValue().format('Y-m-d');
                var date_type = "";
                if (created_d.checked) {
                    date_type = "created_at";
                }
                else if (fly_d.checked) {
                    date_type = "fly_date";
                }
                else if (ticket_out_d.checked) {
                    date_type = "ticket_out_date";
                }
                else if (send_d.checked) {
                    date_type = "send_date";
                }
                
                orderds.baseParams.date_type = date_type;
                orderds.baseParams.t_status = t_status;
                orderds.baseParams.status = status;
                orderds.baseParams.date = date;
                orderds.reload();
                ticketds.baseParams.id = "";
                ticketds.reload();
            }
            else {
                Ext.MessageBox.alert('提示信息', '请将必填项信息录入后再提交.');
            }
        }
    })
    
    var formPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 65,
        width: '100%',
        frame: true,
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [date_field]
            }, {
                columnWidth: .10,
                layout: 'form',
                border: false,
                items: [created_d]
            }, {
                columnWidth: .10,
                layout: 'form',
                border: false,
                items: [fly_d]
            }, {
                columnWidth: .10,
                layout: 'form',
                border: false,
                items: [ticket_out_d]
            }, {
                columnWidth: .10,
                layout: 'form',
                border: false,
                items: [send_d]
            }]
        }, {
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [oStatus]
            }, {
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [ticketStatus]
            }, {
                columnWidth: .5,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    formPanel.render('my_order_form');
    
    //############## 订单Grid ##############
    var ordercm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 115
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '订单类型',
        dataIndex: 'status',
        width: 60,
        renderer: typeRender
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 120
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 60
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 90
    }, {
        header: '预定人',
        dataIndex: 'creator',
        width: 60
    }, {
        header: '预定时间',
        dataIndex: 'created_at',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '出票人',
        dataIndex: 'ticket_outer',
        width: 60
    }, {
        header: '出票时间',
        dataIndex: 'ticket_out_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '送票人',
        dataIndex: 'sender',
        width: 60,
        hidden: true
    }, {
        header: '送票时间',
        dataIndex: 'send_date',
        width: 70
    }, {
        header: '收款人',
        dataIndex: 'cash_checker',
        width: 60,
        hidden: true
    }, {
        header: '收款时间',
        dataIndex: 'cash_get_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
        hidden: true
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    
    }]);

    function typeRender(value, cellmeta, record, rowIndex, columnIndex, store){
        var order_type = "";
        if (record.get("web")!=""){
            order_type += "网络";
        }
        if (record.get("international")!=""){
            order_type += "国际";
        }
        if (record.get("group")!=""){
            order_type += "团体";
        }
        return order_type;
    }
    ordercm.defaultSortable = true;
    var orderds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getMyOrders'
        }),
        reader: orderReader
    });
    
    var ordergridpanel = new Ext.grid.GridPanel({
        el: 'ordersgrid',
        ds: orderds,
        cm: ordercm,
        //sm: sm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 280,
        title: '订单列表',
        bbar: new Ext.PagingToolbar({
            pageSize: 10,
            store: orderds,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录"
        })
    
    });
    ordergridpanel.render();
    //ds.load();
    orderds.load({
        params: {
            start: 0,
            limit: 10
        }
    });
    
    //双击订单查看机票详细
    ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        ticketds.baseParams.id = current_order_id;
        ticketds.reload();
    });
    
    //############ 订单-右键菜单 ##########
    var editOrderItem = new Ext.menu.Item({
        text: '【修改订单】',
        //iconCls: 'album',
        handler: rightMenu1Fn
    });
    var cancelOrderItem = new Ext.menu.Item({
        text: '【取消订单】',
        //iconCls: 'album',
        handler: rightMenu2Fn
    });
    var customerInfoItem = new Ext.menu.Item({
        text: '【查看客户】',
        //iconCls: 'album',
        handler: rightMenu3Fn
    });
    var PNRInfoItem = new Ext.menu.Item({
        text: '【提取PNR】',
        //iconCls: 'album',
        handler: rightMenu4Fn
    });
    var changeCustomerItem = new Ext.menu.Item({
        text: '【订单转换】',
        //iconCls: 'album',
        handler: rightMenu5Fn
    });
    var editFlightItem = new Ext.menu.Item({
        text: '【修改航段】',
        //iconCls: 'album',
        handler: rightMenu6Fn
    });
    var addFlightItem = new Ext.menu.Item({
        text: '【追加航段】',
        //iconCls: 'album',
        handler: rightMenu8Fn
    });
    var joinActivityItem = new Ext.menu.Item({
        handler: rightMenu7Fn,
        //iconCls: 'album',
        text: '【参加活动】'
    });
    var sendSMSItem = new Ext.menu.Item({
        text: '【发送短信】',
        //iconCls: 'album',
        handler: myOrderSendSMS
    });
    var rightClick = new Ext.menu.Menu({
        items: [PNRInfoItem, customerInfoItem, '-', editOrderItem, cancelOrderItem, editFlightItem, addFlightItem, changeCustomerItem, '-', joinActivityItem, sendSMSItem]
    });
    //订单右键菜单
    ordergridpanel.on('rowcontextmenu', function(ordergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        if (current_order_status==ORDER_STATUS_YCP || current_order_status==ORDER_STATUS_YFK || current_order_status==ORDER_STATUS_GQD){
            editFlightItem.setText("【改签】");
        }else{
            editFlightItem.setText("【修改航段】");
        }
        if (current_order_status==ORDER_STATUS_YDD){
            addFlightItem.enable();
        }else{
            addFlightItem.disable();
        }
        if (current_order_status==ORDER_STATUS_YCP || current_order_status==ORDER_STATUS_YFK || current_order_status==ORDER_STATUS_GQD || current_order_status==ORDER_STATUS_YQX || current_order_status==ORDER_STATUS_YZF || current_order_status==ORDER_STATUS_YTD){
            cancelOrderItem.disable();
            if (current_order_status==ORDER_STATUS_YQX || current_order_status==ORDER_STATUS_YZF || current_order_status==ORDER_STATUS_YTD){
                editFlightItem.disable();
            }
        }else{
            cancelOrderItem.enable();
            editFlightItem.enable();
        }
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    function rightMenu1Fn(){
        ModOrder(orderds, current_order_id, ticketds);
    }
    function rightMenu2Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, orderds, ticketds);
    }
    function rightMenu3Fn(){
        var c_id_par = ordersrow.get("customer_id");
        showCustomerInfo(c_id_par);
    }
    function rightMenu4Fn(){
        showPnrInfo(current_order_id);
    }
    function rightMenu5Fn(){
        changeCustomer(orderds, current_order_id, ticketds);
    }
    function rightMenu6Fn(){
        var c_id_par = ordersrow.get("customer_id");
        window.location.href = "/index/phoneshow?id=" + c_id_par + "&oid=" + current_order_id;
    }
    function rightMenu7Fn(){
        joinActivity(current_order_id, orderds, ticketds);
    }
    function rightMenu8Fn(){
        var c_id_par = ordersrow.get("customer_id");
        window.location.href = "/index/phoneshow?id=" + c_id_par + "&oid=" + current_order_id + "&option=add";
    }
    function myOrderSendSMS(){
        var current_link_tel = ordersrow.get("link_tel");
        sendSMS(current_link_tel);
    }
    //### 机票Grid ###
    var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 110
    }, {
        header: '状态',
        dataIndex: 'status_type',
        width: 50
    }, {
        header: '出票状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 60
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 40
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 60
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 60
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 60
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 40
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 60
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 60
    }]);
    ticketcm.defaultSortable = true;
    var ticketds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getTicketsByOrderID'
        }),
        reader: ticketReader
    });
    
    var ticketgridpanel = new Ext.grid.GridPanel({
        el: 'ticketsgrid',
        ds: ticketds,
        cm: ticketcm,
        //sm: sm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 180,
        title: '机票列表'
    });
    
    ticketgridpanel.render();
    ticketds.load();
    
    //############ 票-右键菜单 ##########
    ticketgridpanel.on('rowcontextmenu', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = ticketds.data.items[rowIndex].id;
        ticketsrow = ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        if (current_ticket_status==TICKET_STATUS_YCP){
            drawTicketItem.enable();
            editTicketItem.enable();
            delPassengerItem.disable();
        }else{
            drawTicketItem.disable();
            if (current_ticket_status==TICKET_STATUS_YQX || current_ticket_status==TICKET_STATUS_YZF || current_ticket_status==TICKET_STATUS_YTP){
                //已取消 已作废 已退票
                delPassengerItem.disable();
                editTicketItem.disable();
            }else{
                delPassengerItem.enable();
                editTicketItem.enable();
            }
        }
        
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
    var editTicketItem = new Ext.menu.Item({
        text: '【编辑修改】',
        //iconCls: 'album',
        handler: ticketRightMenu1
    });
    var delPassengerItem = new Ext.menu.Item({
        text: '【取消乘客】',
        //iconCls: 'album',
        handler: ticketRightMenu2
    });
    var drawTicketItem = new Ext.menu.Item({
        text: '【办理退票】',
        //iconCls: 'album',
        handler: ticketRightMenu3
    });
    function ticketRightMenu1(){
        ModTicket(ticketds, current_ticket_id, orderds);
    }
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
        cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, ticketds);
    }
    function ticketRightMenu3(){
        if (current_ticket_status == TICKET_STATUS_YTP) {
            Ext.MessageBox.alert('操作提示', '这张票已经退了');
        }
        else if (current_ticket_status == TICKET_STATUS_YCP) {
            drawTicket(ticketds, current_ticket_id, orderds);
        }
        else {
            Ext.MessageBox.alert('操作提示', '未付款的请选择【取消乘客】');
        }
    }
    var ticketRightClick = new Ext.menu.Menu({
        items: [editTicketItem, delPassengerItem, '-', drawTicketItem]
    });

})
